#	sniffUpdateFile.SCCS.sh
#
#	SNiFF+ V3.0
#
#	Copyright (C) 1994-1998 TakeFive Software GmbH
#	Salzburg, Austria (e-mail: sniff@TakeFive.co.at)
#
#	Author: Weninger Max
#
#	Shell script for SCCS Interface.
#	Update private file in the private working environment: 
#		- touch private file if it is writable and project is shared
#		- or remove private file if shared file is up-to-date
#		- or check out file into private working environment if private 
#		  file is not up-to-date
#
#	Usage: 
#	sniffUpdateFile.SCCS.sh  WorkingDir  FileTypeDir  File  RepositoryDir
# 							 [-r Revision]  SWS [OWS [System [Delimiter [Null-device [TemporaryDir]]]]]
#
#	WorkingDir	- working directory of the file (with file-type directory)
#	FileTypeDir   - file-type directory
#	File		  - file name
#	RepositoryDir - repository directory of the project
#   TemporaryDir  - temporary directory 
#

if [ "$4" = "" ]
then
	echo "Usage: $0  WorkingDir  FileTypeDir  File  RepositoryDir  [-r Revision]  SWS [OWS [System [Delimiter [Null-device [TemporaryDir]]]]]"
	exit 1
fi

WORKDIR=$1
TYPE_DIR=$2
FILE=$3
WORKFILE=$FILE
ROOTDIR=$4/$TYPE_DIR
ARCHIVE=$ROOTDIR/SCCS/s.$FILE
DEBUG=0
SYSTEM=Unix
D=";"
TMPDIR=/tmp
 
if [ "$5" = "-r" ]
then
	REVISION=$6
	if [ "$REVISION" = "" ]
	then
		REVISION=
	else 
		REVISION=-r$6
	fi
	shift 2
else
	REVISION=
fi
 
SWS=$5
 
if [ "$7" != "" ]
then
	SYSTEM=$7
fi
if [ "$8" != "" ]
then
	D=$8
fi
if [ "$D" != " " ]
then
	SWS=`echo "$SWS" | sed -e "s/$D/ /g"`
fi
shift 1
if [ "$9" != "" ]
then
	TMPDIR=$9
fi

if [ "$SYSTEM" = "Windows" ]
then
	IS_WINDOWS=1
else
	IS_WINDOWS=0
fi
 
# VCS-specific command for check out file without lock
CHECK_OUT="cd $WORKDIR; sccs -d$ROOTDIR get -s $REVISION $WORKFILE"

# VCS-specific commands for check whether the files are out-of-date
#DIFF="sccs -d$ROOTDIR diffs $REVISION $WORKFILE | sed -e '1,2d'"
DIFF="sccs -d$ROOTDIR get -s -p $REVISION $WORKFILE > $TMPDIR/$WORKFILE; diff $TMPDIR/$WORKFILE $WORKFILE"
 
cd $WORKDIR

# exit if private file has no repository file

if [ ! -r $ARCHIVE ] 
then
	echo "File @B$FILE@B has no repository file ($ARCHIVE)"
	exit 0
fi

# if private file is writable

if [ -w $WORKFILE ] 
then
	# if project is shared
	if [ "$SWS" != "" ]
	then
		# if private file is a symbolic link
		if [ -h $WORKFILE ]
		then
			exit 0
		fi
		echo "Touching private writable file @B$FILE@B"
		if [ "$DEBUG" = "1" ]
		then
			echo touch $WORKFILE
		fi
		cd $WORKDIR; touch $WORKFILE
	fi
	exit 0
fi

# if private file is writable for other user and it isn't a symbolic link
if [ "$IS_WINDOWS" = "0" ]
then
	if [ -f $WORKFILE -a ! -h $WORKFILE ]
	then
		if [ "`ls -l $WORKFILE | awk '{print $1}' | grep w`" != "" ] 
		then
			echo "Warning: Private file @B$FILE@B is writable for other user"
			exit 0
		fi
	fi
fi

# if project is not shared

if [ "$SWS" = "" ]
then
	# if private file is not up-to-date
	if [ "$DEBUG" = "1" ]
	then
		echo $DIFF
	fi
	cd $WORKDIR; diffrc=`eval $DIFF`
	rm -f $TMPDIR/$WORKFILE
	if [ "$diffrc" = "" ]
	then
		true
	else
		false
	fi
	if [ $? -ne 0 ]
	then
		echo "-> Checking @B$FILE@B out"
		# check out local
		if [ "$DEBUG" = "1" ]
		then
			echo $CHECK_OUT
		fi
		eval $CHECK_OUT
	fi
	exit 0
fi

# project is shared

SHARED_FILE=
# if private file does not exist
if [ ! -r $WORKFILE ]
then
	# file is shared
	# search shared file in SWS
	for DIR in $SWS
	do
		SHARED_FILE=$DIR/$TYPE_DIR/$FILE
		if [ -r $SHARED_FILE ]
		then
			break
		else
			SHARED_FILE=
		fi
	done
	# if shared file is not found
	if [ "$SHARED_FILE" = "" ] 
	then
		# check out local
		echo "-> Checking @B$FILE@B out"
		if [ "$DEBUG" = "1" ]
		then
			echo $CHECK_OUT
		fi
		eval $CHECK_OUT
	else
		if [ "$DEBUG" = "1" ]
		then
			echo $DIFF
		fi
		cd `dirname $SHARED_FILE`; diffrc=`eval $DIFF`
		rm -f $TMPDIR/$WORKFILE
		if [ "$diffrc" = "" ]
		then
			true
		else
			false
		fi
		#  if shared file is not up-to-date
		if [ $? -ne 0 ]
		then
			# check out local
			echo "-> Checking @B$FILE@B out"
			if [ "$DEBUG" = "1" ]
			then
				echo $CHECK_OUT
			fi
			eval $CHECK_OUT
		fi
	fi
	exit 0
fi

# file is in local WS
# search shared file in SWS
for DIR in $SWS
do 
	SHARED_FILE=$DIR/$TYPE_DIR/$FILE
	if [ -r $SHARED_FILE ]
	then
		if [ "$IS_WINDOWS" = "1" ]
		then
			break
		fi
		if [ ! -h $SHARED_FILE ]
		then 
			break
		else
			SHARED_FILE=
		fi
	else
		SHARED_FILE=
	fi
done

# if shared file is not found 
if [ "$SHARED_FILE" = "" ] 
then
	# if private file is not up-to-date
	if [ "$DEBUG" = "1" ]
	then
		echo $DIFF
	fi
	cd $WORKDIR; diffrc=`eval $DIFF`
	rm -f $TMPDIR/$WORKFILE
	if [ "$diffrc" = "" ]
	then
		true
	else
		false
	fi
	if [ $? -ne 0 ]
	then
		# check out local
		echo "-> Checking @B$FILE@B out"
		if [ "$DEBUG" = "1" ]
		then
			echo $CHECK_OUT
		fi
		eval $CHECK_OUT
	fi
	exit 0
fi

if [ "$DEBUG" = "1" ]
then
	echo $DIFF
fi

cd `dirname $SHARED_FILE`; diffrc=`eval $DIFF`
rm -f $TMPDIR/$WORKFILE
if [ "$diffrc" = "" ]
then
	true
else
	false
fi
# if shared file is not up-to-date
if [ $? -ne 0 ]
then
	if [ "$DEBUG" = "1" ]
	then
		echo $DIFF
	fi
	cd $WORKDIR; diffrc=`eval $DIFF`
	rm -f $TMPDIR/$WORKFILE
	if [ "$diffrc" = "" ]
	then
		true
	else
		false
	fi
	# if private file is not up-to-date
	if [ $? -ne 0 ]
	then
		# check out local
		echo "-> Checking @B$FILE@B out"
		if [ "$DEBUG" = "1" ]
		then
			echo $CHECK_OUT
		fi
		eval $CHECK_OUT
	else
		# if private file is a symbolic link
		if [ -h $WORKFILE ]
		then
			exit 0
		fi
		# private file is up-to-date but shared not
		echo "Touching private read-only file @B$FILE@B"
		if [ "$DEBUG" = "1" ]
		then
			echo touch $WORKFILE
		fi
		cd $WORKDIR; touch $WORKFILE
	fi
	exit 0
fi

# shared file is up-to-date
if [ -h $WORKFILE ]
then
	exit 0
fi

# remove local
if [ "$DEBUG" = "1" ]
then
	echo rm -f $WORKFILE
fi

cd $WORKDIR; rm -f $WORKFILE

if [ $? = 0 ]
then
	echo "Private file @B$FILE@B is deleted, shared file is used"
	exit 0
fi

# file cannot be deleted
echo "Private file @B$FILE@B cannot be deleted"
exit 1
